package com.yamanyar.weebill.domain;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

import org.hibernate.validator.NotNull;

/**
 * <code>Attachment</code> is an entity that can be owned by any other entity.
 * It does not have a reference to a specific object. A <code>Project</code>,
 * a <code>Component</code> (etc.) can have a reference to this object.
 * 
 * Also this entity does not hold the data it self rather it has a reference to
 * data perhaps which is in file system.
 * 
 * 
 * @author Kaan Yamanyar
 */
@Entity
public class Attachment {
	private Long id;

	private String name;

	private String location;

	/**
	 * 
	 * @param location
	 *            Location of the file. It might be a value like
	 *            "\media\downloads\abc.txt" or "http://rep/abc.txt".Maximum
	 *            allowed length is 200 chars.
	 */
	public void setLocation(String location) {
		this.location = location;
	}

	/**
	 * 
	 * @return The name of the attachment. Maximum allowed length is 60 chars.
	 */

	@Column(length = 60)
	@NotNull
	public String getName() {
		return this.name;
	}

	/**
	 * 
	 * @param name
	 *            The name of the attachment. Maximum allowed length is 60
	 *            chars.
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * 
	 * @return The id of the attachment. Id is auto-generated by db.
	 */
	@Id
	@GeneratedValue
	public Long getId() {
		return this.id;
	}

	@Override
	public String toString() {
		return this.name;
	}

	@Override
	public boolean equals(Object obj) {
		if (obj == null || !(obj instanceof Attachment)) {
			return false;
		}
		Attachment that = (Attachment) obj;
		return this.name.equals(that.getName())
				&& this.location.equals(that.getLocation());
	}

	@Override
	public int hashCode() {
		if (this.name == null) {
			return 2500;
		}
		return name.hashCode();
	}

	/**
	 * This method is not supposed to be called by a developer.
	 * 
	 * @param id
	 */
	public void setId(Long id) {
		this.id = id;
	}

	/**
	 * 
	 * @return Location of the file. It might be a value like
	 *         "\media\downloads\abc.txt" or "http://rep/abc.txt". Maximum
	 *         allowed length is 200 chars.
	 */
	@NotNull
	@Column(length = 200)
	public String getLocation() {
		return this.location;
	}

}
